Skip to content

Conversation

@AnnieH
Copy link

@AnnieH AnnieH commented Sep 9, 2013

Question:
I have the latest version of RSpec installed and when I ran tests, it gave me the warning

DEPRECATION: mock is deprecated. Use double instead.

Other sources said 'mock' was just an alias for 'double' anyways but I couldn't figure out how to use 'double' correctly. I suspect it has a slightly different function than mock as well. Can you give any guidance on that?

@jwo
Copy link
Member

jwo commented Sep 9, 2013

DEPRECATION: mock is deprecated. Use double instead.

Yeah, double, mock, and stub have historically mean aliases to each other. I take them to mean:

  • double: an object you want to use in place of a real object for the use of testing. Think a spy double-agent
  • mock: a test-double that you intend to add a message expectation to (test a method was called on it)
  • stub: literally same as double. Confusing since you stub methods out.

As far as rspec deprecating mock, I think that's booooo 👎 but it's their project.

to use it:

double(:name, methods)

so, if you wanted a user that you'll call "Mary" that has a name and email, you could:

double(:mary, {name: 'Mary Johnson', email: 'mary9425@hotmail.com'})

A possible use:

require 'rspec'


class User
  def self.deliquent_process!
    deliquent_users.each do |user|
      user.send_deliquent_notice
    end
  end
end

describe 'emails deliquent invoices' do

  let(:mary)  { double(:mary, {name: 'Mary Johnson', email: 'mary9425@hotmail.com'}) }

  it 'will email mary' do
    User.stub('deliquent_users') { [mary] }
    mary.should_receive(:send_deliquent_notice)
    User.deliquent_process!
  end
end

If you run this, it'll pass. but, it'll pass all the same if you replace the mary with:

let(:mary)  { double }

it's because it's a throwaway holder. We just care that the "thing" representing a user gets called.

help?

@jwo
Copy link
Member

jwo commented Sep 9, 2013

The code you submitted on the assignment looks great -- no improvement needed!

@AnnieH
Copy link
Author

AnnieH commented Sep 11, 2013

Ah, thanks for that explanation and the example. Very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants